package y_2025.m05.d23;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StudentInfoProcessor {
    public static void main(String[] args) {
        String inputFile = "studentInfo.txt";
        String outputFile = "studentName.txt";
        
        try {
            // 1. 读取输入文件并写入输出文件
            processStudentInfo(inputFile, outputFile);
            
            // 2. 获取输出文件信息
            getFileInfo(outputFile);
            
        } catch (IOException e) {
            System.err.println("处理文件时出错: " + e.getMessage());
        }
    }
    
    private static void processStudentInfo(String inputFile, String outputFile) throws IOException {
        try (BufferedReader reader = new BufferedReader(new FileReader(inputFile));
             BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) {
            
            String line;
            int lineNumber = 1;
            
            while ((line = reader.readLine()) != null) {
                // 分割每行内容
                String[] parts = line.split(",");
                
                // 第一个元素是姓名
                if (parts.length > 0) {
                    String name = parts[0].trim();
                    
                    // 写入带行号的姓名
                    writer.write(lineNumber + ". " + name);
                    writer.newLine();
                    
                    lineNumber++;
                }
            }
            
            System.out.println("成功写入学生姓名到 " + outputFile);
        }
    }
    
    private static void getFileInfo(String filePath) throws IOException {
        Path path = Paths.get(filePath);
        
        // 获取文件长度（字节数）
        long fileSize = Files.size(path);
        System.out.println("文件长度: " + fileSize + " 字节");
        
        // 获取文件修改时间
        BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
        Date modifyTime = new Date(attrs.lastModifiedTime().toMillis());
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("文件修改时间: " + sdf.format(modifyTime));
    }
}